<?php

namespace App\ApiBackend\Menu;

use App\Http\Controllers\ApiBaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 *  菜单添加更新
 */
class AddUpdate extends ApiBaseController
{


  /**
   * 参数检查
   */
  protected function check(): void
  {
    // 定义规则
    $ruleArr = [
      'title' => 'required',
      'pid' => 'required',
      'status' => 'required',
    ];
    if (request('id') > 0) {
      $ruleArr['name'] = "required";
    } else {
      $ruleArr['name'] = "required|unique:admin_menu";
    }
    $messageArr = [];
    //验证
    $validator = Validator::make(request()->all(), $ruleArr, $messageArr, [
      "name" => "菜单名称"
    ]);
    //验证失败
    if ($validator->stopOnFirstFailure()->fails()) {

      $this->result["code"] = 1;
      $this->result["message"] = "预检: " . $validator->errors()->first();
    }
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    $parentId = request('pid');
    $isUpdate = $menuId = request('id');
    //数据处理
    $insertData = [
      "title" => request('title'),
      "name" => request('name'),
      "path" => request('path'),
      "pid" => $parentId,
      "type" => request('type'),
      "orderNo" => request('orderNo'),
      "status" => request('status'),
      "icon" => request('icon'),
      "isHide" => request('isHide'),
      "isLink" => request('isLink') ?? 0,
    ];
    switch (request('type')) {
      case 0:
        $insertData['component'] = "LAYOUT";
        break;
      case 1:
        $insertData['component'] = request('component');
        break;
    }
    //更新数据
    try {
      if ($isUpdate) {//更新
        $insertData['updateTime'] = date('Y-m-d H:i:s');
        DB::table('admin_menu')->where('id', $menuId)->update($insertData);
        $this->result["message"] = "更新菜单成功";
      } else {//新增
        $insertData['createTime'] = date('Y-m-d H:i:s');
        DB::table('admin_menu')->insert($insertData);
        $this->result["message"] = "新增菜单成功";
      }

    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = "错误: " . $exception->getMessage();
    }
    return;

  }
}
